package com.xjscrm.console.service.customer.chat;

import cn.hutool.core.util.IdUtil;
import com.xjscrm.common.annotation.MerId;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.common.util.LoginContext;
import com.xjscrm.console.dto.chat.ChatWordsLibraryGroupOptDTO;
import com.xjscrm.console.dto.chat.ChatWordsLibraryGroupOrderDTO;
import com.xjscrm.console.dto.chat.ChatWordsLibraryGroupQueryDTO;
import com.xjscrm.console.entity.ChatWordsLibraryGroup;
import com.xjscrm.console.mapper.ChatWordsLibraryGroupMapper;
import com.xjscrm.console.mapper.ChatWordsLibraryMapper;
import com.xjscrm.console.vo.LoginVO;
import com.xjscrm.console.vo.chat.ChatWordsLibraryGroupVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author mingyang.chen create in 2024/3/15 11:59
 */
@Service
public class ChatWordsLibraryGroupService {

    @Resource
    private ChatWordsLibraryGroupMapper chatWordsLibraryGroupMapper;

    @Resource
    private ChatWordsLibraryMapper chatWordsLibraryMapper;


    public int deleteByPrimaryKey(Long id) {
        return chatWordsLibraryGroupMapper.deleteByPrimaryKey(id);
    }


    public int insertSelective(ChatWordsLibraryGroup record) {
        return chatWordsLibraryGroupMapper.insertSelective(record);
    }


    public ChatWordsLibraryGroup selectByPrimaryKey(Long id) {
        return chatWordsLibraryGroupMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(ChatWordsLibraryGroup record) {
        return chatWordsLibraryGroupMapper.updateByPrimaryKeySelective(record);
    }

    public Object search(LoginVO login, ChatWordsLibraryGroupQueryDTO query) {

        List<ChatWordsLibraryGroup> wordsLibraryGroupList = chatWordsLibraryGroupMapper.query(login.getMerId(), login.getId(), query);

        if (ObjectUtils.isEmpty(wordsLibraryGroupList)) {
            return wordsLibraryGroupList;
        }
        return wordsLibraryGroupList.stream().map(item -> {
            ChatWordsLibraryGroupVO chatWordsLibraryVO = new ChatWordsLibraryGroupVO();
            BeanUtils.copyProperties(item, chatWordsLibraryVO);
            return chatWordsLibraryVO;
        }).collect(Collectors.toList());
    }

    public void add(LoginVO login, ChatWordsLibraryGroupOptDTO opt) {

        this.doAdd(opt.getType(), opt.getName(), opt.getParentId());
//        Integer sort = chatWordsLibraryGroupMapper.selectMaxSort(login.getMerId(), login.getId());
//
//        ChatWordsLibraryGroup wordsLibraryGroup = new ChatWordsLibraryGroup();
//        BeanUtils.copyProperties(opt, wordsLibraryGroup);
//        wordsLibraryGroup.setId(IdUtil.getSnowflakeNextId());
//        wordsLibraryGroup.setStaffId(login.getId());
//        wordsLibraryGroup.setMerId(login.getMerId());
//        wordsLibraryGroup.setParentId(ObjectUtils.defaultIfNull(opt.getParentId(), 0L));
//        wordsLibraryGroup.setSort(Objects.isNull(sort) ? 0 : sort + 1);
//        wordsLibraryGroup.setCreateBy(login.getId());
//        wordsLibraryGroup.setUpdateBy(login.getId());
//        wordsLibraryGroup.setCreateTime((int) (System.currentTimeMillis() / 1000L));
//        wordsLibraryGroup.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
//
//        chatWordsLibraryGroupMapper.insertSelective(wordsLibraryGroup);
    }

    public void edit(LoginVO login, ChatWordsLibraryGroupOptDTO opt) {
        ChatWordsLibraryGroup edit = new ChatWordsLibraryGroup();
        BeanUtils.copyProperties(opt, edit);
        edit.setId(opt.getId());
        edit.setUpdateBy(login.getId());
        edit.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
        chatWordsLibraryGroupMapper.updateByPrimaryKeySelective(edit);
    }

    public void delete(LoginVO login, Long id) {

        int count = chatWordsLibraryMapper.countByGroupId(login.getMerId(), id);
        if (count > 0) {
            throw new BusinessException("该话术分组下存在话术, 不支持删除");
        }
        chatWordsLibraryGroupMapper.deleteByPrimaryKey(id);
    }

    public void sort(LoginVO login, ChatWordsLibraryGroupOrderDTO opt) {

        for (ChatWordsLibraryGroupOrderDTO.GroupItem sortItem : opt.getSortItems()) {
            ChatWordsLibraryGroup update = new ChatWordsLibraryGroup();
            update.setId(sortItem.getId());
            update.setSort(sortItem.getSort());
            update.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
            chatWordsLibraryGroupMapper.updateByPrimaryKeySelective(update);
        }
    }

    public ChatWordsLibraryGroup getByNameIfAbsentAdd(@MerId Long merId, Integer type, String groupName) {
        LoginVO login = LoginContext.getLogin();
        ChatWordsLibraryGroup chatWordsLibraryGroup = chatWordsLibraryGroupMapper.selectByNameAndType(merId, type, groupName, Objects.equals(type, 11) ? login.getId() : null);
        if (Objects.isNull(chatWordsLibraryGroup)) {
            Long id = this.doAdd(type, groupName, null);
            return selectByPrimaryKey(id);
        }
        return chatWordsLibraryGroup;
    }

    private Long doAdd(Integer type, String groupName, Long parentId) {

        LoginVO login = LoginContext.getLogin();
        Integer sort = chatWordsLibraryGroupMapper.selectMaxSort(login.getMerId(), login.getId());

        ChatWordsLibraryGroup wordsLibraryGroup = new ChatWordsLibraryGroup();
        wordsLibraryGroup.setId(IdUtil.getSnowflakeNextId());
        wordsLibraryGroup.setStaffId(login.getId());
        wordsLibraryGroup.setMerId(login.getMerId());
        wordsLibraryGroup.setType(type);
        wordsLibraryGroup.setName(groupName);
        wordsLibraryGroup.setParentId(ObjectUtils.defaultIfNull(parentId, 0L));
        wordsLibraryGroup.setSort(Objects.isNull(sort) ? 0 : sort + 1);
        wordsLibraryGroup.setCreateBy(login.getId());
        wordsLibraryGroup.setUpdateBy(login.getId());
        wordsLibraryGroup.setCreateTime((int) (System.currentTimeMillis() / 1000L));
        wordsLibraryGroup.setUpdateTime((int) (System.currentTimeMillis() / 1000L));

        chatWordsLibraryGroupMapper.insertSelective(wordsLibraryGroup);
        return wordsLibraryGroup.getId();
    }
}
